package com.notes.designpattern.chain;

import com.google.common.collect.Lists;
import com.notes.designpattern.chain.domain.BaseBO;
import com.notes.designpattern.chain.domain.BaseReq;
import com.notes.designpattern.chain.domain.BaseResp;

import java.util.List;

public class BusinHandlerChain {

    List<BusinHandler> businHandlerList = Lists.newArrayList();

    int index = 0;

    public BusinHandlerChain addBusinHandler(BusinHandler businHandler) {
        this.businHandlerList.add(businHandler);
        return this;
    }

    public BaseResp process(BaseReq baseReq, BaseBO baseBO, BusinHandlerChain businHandlerChain) {
        if (index == businHandlerList.size()) {
            System.out.println("业务执行结束，执行成功业务数：" + index);
            BaseResp baseResp = new BaseResp();
            return baseResp;
        }

        BusinHandler businHandler = businHandlerList.get(index);
        index++;
        BaseResp processResp = businHandler.process(baseReq, baseBO, businHandlerChain);

        return processResp;
    }
}
